<template>
    <view class="bottom-bar cart-bottom-bar" v-if="total">
        <text class="item-radio" v-if="false"> {{ total.item }}种{{ total.amount }}件 </text>
        <view class="item-mian">
            <view class="price">
                <view>
                    <text>合计:</text>
                    <text class="price-text">{{ payPrice }}</text>
                    <text class="price-text small">元</text>
                </view>
            </view>
            <button class="btn btn-success" :class="{ bordernone: submitOne }" :disabled="submitOne" @click="pay">
                提交订单
            </button>
        </view>
        <uni-popup type="bottom" ref="payWay" background-color="#fff" :safe-area="false">
            <pay-way :payPrice="String(payPrice)" v-if="payList.length > 0" :payList="payList" @cancel="cancel"
                @select="select"></pay-way>
        </uni-popup>
    </view>
</template>

<script>
import { mapMutations, mapState, mapGetters } from "vuex"
import { submitOrderByUserCar, offlinePay } from "@/service/api/order.js"
import storage from "@/uni-sub-base/utils/storage.js"
import { getUserInfo } from "@/utils/appInit.js"
import { getUserCarList } from "@/utils/cartUtil.js"
import payWay from "@/components/common/pay-way.vue"
import store from "@/store/index.js"
import config from "@/config/config.js"
import orderType from "@/type/orderType.js"
export default {
    components: {
        payWay,
    },
    props: {
        orderData: {
            type: Object,
            default: {},
        },
        webInfo: {
            type: Object,
            default: {},
        },
        TakeDelivery: {
            type: Object,
            default: {},
        },
        useAddress: {
            type: Object,
            default: {},
        },
        ids: {
            type: String,
            default: "",
        },
        payList: {
            type: Array,
            default: [],
        },
        carPolicyList: {
            type: Array,
            default: [],
        },
        postPrice: {
            type: Number,
            default: 0,
        },
        orderType: {
            type: Number,
            default: 0,
        },
        isTempCar: {
            type: Boolean,
            default: false,
        },
        remark: {
            type: String,
            default: "",
        },
        deliveryDate: {
            type: String,
            default: "",
        },
        payPrice: {
            type: String,
            default: 0,
        },
        useCoupon: {
            type: Object,
            default: null,
        },
        activityId: {
            type: String,
            default: 0,
        },
        isUsePoint: {
            type: Boolean,
            default: false,
        },
        isLimit: {
            type: Boolean,
            default: false,
        },
        projectId: {
            type: Number,
            default: 0,
        },
        MainOrderId: {
            type: Number,
            default: 0,
        },
        project: {
            type: Number,
            default: 0,
        },
        postType: {
            type: Number,
            default: 0,
        },
        postTypeName: {
            type: String,
            default: "",
        },
        wareHouseId: {
            type: String,
            default: "",
        },
        PackageBuyId: {
            type: String,
            default: "",
        }
    },
    data() {
        return {
            total: null,
            balance: 0,
            submitOne: false,
        }
    },
    mounted() {
        this.getInit()
    },
    methods: {
        ...mapMutations(["UP_CART_VIEW", "UP_ORDER_VIEW"]),
        //获取总额
        getTotal(postPrice) {
            //加上邮费
            if (this.total.price) {
                let total = Number(this.total.price)
                return total + postPrice
            }
            return 0
        },
        //初始化数据
        getInit() {
            let totalPrice = 0
            let totalItem = this.orderData.List.length
            let totalAmount = 0
            this.orderData.List.forEach((e) => {
                e.CarList.forEach((item) => {
                    totalPrice += item.TotalMoney
                    totalAmount += item.Amount
                })
                totalPrice = totalPrice - e.Discount
            })
            this.total = {
                item: totalItem,
                price: totalPrice.toFixed(2),
                amount: totalAmount,
            }
            this.$forceUpdate()
        },
        //点击支付
        pay() {
            if (this.checkAddress()) {
                return
            }
            this.submitOrder()
        },
        //查检收货地址
        checkAddress() {
            if (!this.useAddress || !this.useAddress.ID) {
                this.easyToast("请先选择收货地址")
                return true
            }
            return false
        },
        //提交订单
        submitOrder(payItem) {
            //检查收货地址
            if (this.checkAddress()) {
                return
            }
            if (this.project == 1 && this.projectId == 0) {
                this.easyToast("请先选择关联项目")
                return true
            }
            let webInfo = storage.getCache("webInfo")
            if (webInfo.IsSelectPostType == 1 && this.postType == 0 && this.orderType != 23) {
                this.easyToast("请先选择配送方式")
                return true
            }
            if (this.webInfo.IsSubmitRemark == 1) {
                if (this.remark == "") {
                    this.easyToast("请填写买家留言")
                    return
                }
            }
            if (webInfo.IsSubmitDeliveryDate == 1) {
                if (this.deliveryDate == "") {
                    this.easyToast("请选择交货时间")
                    return
                }
            }

            // //检查余额,余额是否够
            // if (payItem.PaymentPayWay == 1 && (payItem.Balance <= 0 || payItem.Balance < this.total.price)) {
            // 	this.easyToast("您的余额不足，请前往充值再提交订单")
            // 	return
            // }
            // //检查信用余额是否够
            // if (payItem.PaymentPayWay == 10 && (payItem.Balance <= 0 || payItem.Balance < this.total.price)) {
            // 	this.easyToast("您的信用余额不足")
            // 	return
            // }

            let param = {
                carIds: this.ids,
                orderType: 4,
                // "payType": payItem.PayType,
                // "payWay": payItem.PaymentPayWay,
                fromType: 1,
                infoType: 1,
                storeId: 0,
                // "isBalancePay": payItem.PaymentPayWay == 1,
                addressId: this.useAddress.ID,
                balance: this.balance,
                isTempCar: this.isTempCar,
                orderPolicy: {
                    carPolicyList: this.carPolicyList,
                    orderGiftList: [],
                },
                isUsePoint: this.isUsePoint,
                remark: this.remark,
                IsCheckCustomForm: true,
                PostType: this.postType,
                PostTypeName: this.postTypeName,
                DeliveryTime: this.deliveryDate,
            }
            if (this.MainOrderId > 0) {
                param.MainOrderId = this.MainOrderId
            }
            if (this.activityId && this.activityId != "0") {
                // param.orderType = orderType.activity
                param.activityId = Number(this.activityId)
            } else if (this.isLimit) {
                param.orderType = orderType.limit
            }
            if (this.orderType > 0) {
                param.orderType = this.orderType
                if (this.orderType == 23) {
                    param.takeDeliveryId = this.TakeDelivery.ID
                }
            }
            if (this.useCoupon) {
                param.couponId = this.useCoupon.ID
                param.couponMoney = this.useCoupon.CouponPrivilegeValue
            }
            if (this.projectId > 0) {
                param.projectId = this.projectId
            }
            if (webInfo.IsSelectWarehouse == 1) {
                let warehouse = storage.getCache("warehouse")
                param.warehouseId = warehouse.WarehouseId
            }
            if (this.wareHouseId) {
                param.warehouseId = this.wareHouseId
            }
            if (this.PackageBuyId) {
                param.PackageBuyId = this.PackageBuyId
            }
            uni.showLoading({
                title: "加载中",
            })
            if (!this.submitOne) {
                this.submitOne = true
                //提交订单接口
                submitOrderByUserCar(param).then((res) => {
                    if (res.State == 1) {
                        uni.hideLoading()
                        //埋点
                        let tackData = {
                            id: res.Data.ID,
                            orderPrice: res.Data.PayMoney,
                        }
                        this.tackSubmitOrder(tackData)
                        //更新购物车
                        getUserCarList().then((res) => { })
                        if (this.activityId) {
                            //清除临时数据
                            store.commit("SET_TEMP_CART", [])
                        }
                        //支付
                        if (this.payPrice > 0) {
                            this.easyRemoveCurrentGoTo(
                                "/pages_shop/packet/order/waitPayList?orderId=" + res.Data.OrderId,
                            )
                        } else {
                            this.easyReLaunch("/pages_shop/packet/order/payResult?orderId=" + res.Data.OrderId)
                        }
                        setTimeout((e) => {
                            this.submitOne = false
                        }, 500)
                    } else {
                        uni.hideLoading()
                        uni.showModal({
                            title: "提示",
                            content: res.Msg,
                            showCancel: false,
                        })
                        if (res.Msg.includes("未填写")) {
                            this.$emit("formNoEmptyChange", true)
                        }
                        setTimeout((e) => {
                            this.submitOne = false
                        }, 500)
                    }
                })
            }
        },
        cancel() {
            this.$refs.payWay.close()
        },
        select(payItem) {
            this.submitOrder(payItem)
            this.cancel()
        },
        upView() {
            //通知外面更新
            this.UP_CART_VIEW(new Date().getTime())
            this.UP_ORDER_VIEW(new Date().getTime())
        },
    },
}
</script>

<style lang="scss">
.cart-bottom-bar {
    flex-direction: row;
    z-index: 9;

    .item-mian {
        display: flex;
        flex-direction: row;
        align-items: center;
        flex: 1;
    }

    .price {
        display: flex;
        flex: 1;
        flex-direction: column;
        align-items: flex-end;
        justify-content: flex-end;
        margin-right: 20rpx;

        .price-text {
            font-size: $font-size-36;
            font-weight: 700;
            color: $app-color-main;
            margin-left: 20rpx;
        }

        .small {
            margin-left: 6rpx;
            font-size: $font-size-24;
            margin-bottom: 6rpx;
            color: $color-gray-1;
        }

        .num {
            text {
                margin-left: 10rpx;
            }
        }
    }

    .btn {
        width: 220rpx;
        height: 80rpx;
        background: $app-color-main;
        border-radius: 45rpx;
        color: #ffffff;
        font-size: 32rpx;
    }
}

.bordernone {
    border-color: #ccc !important;
}
</style>
